package gen1us.jutw.CreateConfig;

import java.util.ArrayList;

/*
 Copyright 2009 Evgeny Yusov

 This file is part of JUTW.

 JUTW is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 JUTW is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with JUTW.  If not, see http://www.gnu.org/licenses/.
 */

class ComboBox implements Cloneable {

	public class MenuItem {
		private class Pair {

			private final String param;

			private final String value;

			public Pair(final String param, final String value) {
				this.param = param;
				this.value = value;
			}

			public String getParam() {
				return param;
			}

			public String getValue() {
				return value;
			}
		}

		private final String label;

		private final ArrayList<Pair> params = new ArrayList<Pair>();

		public MenuItem(final String label, final String param,
				final String value) {
			this.label = label;
			addParam(param, value);
		}

		public void addParam(final String param, final String value) {
			params.add(new Pair(param, value));
		}

		public void clear() {
			params.clear();
		}

		public String getLabel() {
			return label;
		}

		public String getParam(final int i) {
			final Pair tmpPair = params.get(i);
			return tmpPair.getParam();
		}

		public int getSize() {
			return params.size();
		}

		public String getValue(final int i) {
			final Pair tmpPair = params.get(i);
			return tmpPair.getValue();
		}
	}

	private final String desc;

	private final ArrayList<MenuItem> items = new ArrayList<MenuItem>();

	private MenuItem lastItem;

	public ComboBox(final String desc) {
		this.desc = desc;
	}

	public void add(final String label, final String param, final String value) {
		final MenuItem tmpItem = getItem(label);
		final MenuItem item = new MenuItem(label, param, value);
		if (tmpItem == null) {
			items.add(item);
		} else
			tmpItem.addParam(param, value);
	}

	public void addLIParam(final String param, final String value) {
		if (lastItem == null)
			lastItem = new MenuItem("", param, value);
		else
			lastItem.addParam(param, value);
	}

	public void clear() {
		items.clear();
	}

	public void clearLI() {
		if (lastItem != null)
			lastItem.clear();
	}

	public String getDesc() {
		return desc;
	}

	private MenuItem getItem(final String str) {
		for (final MenuItem i : items)
			if (i.getLabel().equals(str))
				return i;
		return null;
	}

	public String[] getItems() {
		final int size = items.size();
		final String[] itemsStr = new String[size];
		for (int i = 0; i < size; i++) {
			itemsStr[i] = items.get(i).getLabel();
		}
		return itemsStr;
	}

	public int getLINumberParams() {
		if (lastItem != null)
			return lastItem.getSize();
		else
			return 0;
	}

	public String getLIParam(final int j) {
		return lastItem.getParam(j);
	}

	public String getLIValue(final int j) {
		return lastItem.getValue(j);
	}

	public int getNumberParams(final String s) {
		final MenuItem tmpItem = getItem(s);
		return tmpItem.getSize();
	}

	public String getParam(final String s, final int i) {
		final MenuItem tmpItem = getItem(s);
		return tmpItem.getParam(i);
	}

	public String getValue(final String s, final int i) {
		final MenuItem tmpItem = getItem(s);
		return tmpItem.getValue(i);
	}
}
